package com.junci.mtest.utils;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;
import java.util.List;

/**
 * SharePreferences操作工具类
 */
public class SharePrefUtil {
    private static String tag                             = SharePrefUtil.class.getSimpleName();
    private final static String SP_NAME                         = "qianka";

    public final static String SP_USER_INFO_SAVE_KEY           = "sp_user_info_save_key"; // 用户信息


    public final static String SEPARATOR = "######";    // 分割线

    private static SharedPreferences sp;

    public static void init(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
    }


    /**
     * 保存布尔值
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveBoolean(Context context, String key, boolean value) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        sp.edit().putBoolean(key, value).apply();
    }

    /**
     * 保存字符串
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveString(Context context, String key, String value) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        if (LogUtil.isLog()) {
            LogUtil.s(" 这是底层SP保存数据的地方： key：" + key + "   value:" + value);
        }
        sp.edit().putString(key, value).apply();
    }

    /**
     * 清空这个文件
     *
     * @param context
     */
    public static void clear(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        sp.edit().clear().apply();
    }

    /**
     * 移除指定的键值
     *
     * @param context
     * @param key
     */
    public static void remove(Context context, String key) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        sp.edit().remove(key).apply();
    }

    /**
     * 保存long型
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveLong(Context context, String key, long value) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        sp.edit().putLong(key, value).apply();
    }

    /**
     * 保存int型
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveInt(Context context, String key, int value) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        sp.edit().putInt(key, value).apply();
    }

    /**
     * 保存float型
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveFloat(Context context, String key, float value) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        sp.edit().putFloat(key, value).apply();
    }

    /**
     * 获取字符值
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static String getString(Context context, String key, String defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        return sp.getString(key, defValue);
    }

    /**
     * 获取int值
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static int getInt(Context context, String key, int defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        return sp.getInt(key, defValue);
    }

    /**
     * 获取long值
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static long getLong(Context context, String key, long defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        return sp.getLong(key, defValue);
    }

    /**
     * 获取float值
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static float getFloat(Context context, String key, float defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        return sp.getFloat(key, defValue);
    }

    /**
     * 获取布尔值
     *
     * @param context  s
     * @param key      s
     * @param defValue s
     * @return s
     */
    public static boolean getBoolean(Context context, String key,
                                     boolean defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        return sp.getBoolean(key, defValue);
    }


    /**
     * 获取String数组
     *
     * @param context  s
     * @param key      s
     * @param defValue s
     * @return s
     */
    public static String[] getStringArray(Context context, String key,
                                          String[] defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        String string = sp.getString(key, "");
        //        //System.out.println("   获取到的数据是：" + string);
        if ("".equals(string)) {
            return defValue;
        }
        return string.split(SEPARATOR);
    }


    /**
     * 存储string数组
     *
     * @param context s
     * @param key     s
     * @param value   s
     * @return s
     */
    public static void saveStringArray(Context context, String key,
                                       String[] value) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        if (value != null) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < value.length; i++) {
                sb.append(value[i]);
                if (i != value.length - 1) {
                    sb.append(SEPARATOR);
                }
            }
            //            //System.out.println("  存储的数据的最终形态：" + sb.toString());
            sp.edit().putString(key, sb.toString()).apply();
        }
    }


    /**
     * 获取String集合
     *
     * @param context  s
     * @param key      s
     * @param defValue s
     * @return s
     */
    public static List<String> getStringList(Context context, String key,
                                             List<String> defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        String string = sp.getString(key, "");
        //        //System.out.println("   获取到的数据是：" + string);
        if ("".equals(string)) {
            return defValue;
        }
        String[] split = string.split(SEPARATOR);
        List<String> test = new ArrayList<String>();
        for (String t : split) {
            test.add(t);
        }
        return test;
    }


    /**
     * 存储string集合
     *
     * @param context s
     * @param key     s
     * @param value   s
     * @return s
     */
    public static void saveStringList(Context context, String key,
                                      List<String> value) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        if (value != null) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < value.size(); i++) {
                sb.append(value.get(i));
                if (i != value.size() - 1) {
                    sb.append(SEPARATOR);
                }
            }
            //            //System.out.println("  存储的数据的最终形态：" + sb.toString());
            sp.edit().putString(key, sb.toString()).apply();
        }
    }

}
